package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.WebActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.NewsEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class NewBannerAdapter extends BannerAdapter<NewsEntity.DataBean.BannerListBean, NewBannerAdapter.BannerViewHolder> {
    private Context context;

    public NewBannerAdapter(Context context, List<NewsEntity.DataBean.BannerListBean> datas) {
        super(datas);
        this.context = context;
    }

    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.banner_news, null);
        BannerViewHolder banner_news = new BannerViewHolder(view);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        return banner_news;
    }

    @Override
    public void onBindView(BannerViewHolder holder, NewsEntity.DataBean.BannerListBean data, int position, int size) {
        RoundedCornersImage image = holder.itemView.findViewById(R.id.rci_cover);
        TextView tv_title = holder.itemView.findViewById(R.id.tv_title);

        if (!TextUtils.isEmpty(data.cover)) {
            GlideUtils.loadImage(context, data.cover, image);
        }

        tv_title.setText(data.title);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra("LINK_URL",
                        String.format(Constant.H5_CONTANST_DETAIL, UserAPI.getUserToken(), data.informationId));
                context.startActivity(intent);
            }
        });
    }

    public class BannerViewHolder extends RecyclerView.ViewHolder {
        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
